package org.xxf.tdengine.demo.taos;

import cn.hutool.core.util.RandomUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

public class TaosDemo {

    public static void main(String[] args) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:TAOS-WS://192.168.52.132:6041/soundanaly?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8");
        config.setUsername("root");
        config.setPassword("Qwe123@awa");
        // maximum-pool-size
        config.setMinimumIdle(10); // minimum number of idle connection
        config.setMaximumPoolSize(10); // maximum number of connection in the pool
        config.setConnectionTimeout(30000); // maximum wait milliseconds for get connection from pool
        config.setMaxLifetime(0); // maximum life time for each connection
        config.setIdleTimeout(0); // max idle time for recycle idle connection
        config.setConnectionTestQuery("SELECT SERVER_VERSION()"); // validation query

        HikariDataSource dataSource = new HikariDataSource(config);

        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT into lp(tbname,ts,lafp,lasp,laip,lcfp,lcsp,lcip,lzfp,lzsp,lzip,model,effecTIve,measure_id,product_key) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            long unix = System.currentTimeMillis();
            for (int i = 0; i < 100; i++) {
                long time  = unix - i * 1000;

                preparedStatement.setString(1 , i % 2 == 0 ? "lp_360695" : "lp_600068");
                preparedStatement.setLong(2 , time);
                preparedStatement.setFloat(3 , randomDouble());
                preparedStatement.setFloat(4 , randomDouble());
                preparedStatement.setFloat(5 , randomDouble());
                preparedStatement.setFloat(6 , randomDouble());
                preparedStatement.setFloat(7 , randomDouble());
                preparedStatement.setFloat(8 , randomDouble());
                preparedStatement.setFloat(9 , randomDouble());
                preparedStatement.setFloat(10 , randomDouble());
                preparedStatement.setFloat(11 , randomDouble());
                preparedStatement.setString(12 , "狗叫");
                preparedStatement.setBoolean(13 , i % 2 == 0);
                preparedStatement.setLong(14 , 3333);
                preparedStatement.setString(15 , "AWA3000");
                preparedStatement.addBatch();
            }
            System.out.println("3");
            preparedStatement.executeBatch();
            System.out.println("2");
            preparedStatement.close();
            System.out.println("1");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            System.out.println("over");
        }
    }

    private static float randomDouble(){
        return RandomUtil.randomInt(600,900) / 10f;
    }
}
